package com.group.converter;

import org.springframework.core.convert.converter.Converter;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * com.gxa.op.converter
 * User: 恩哲欣
 * Date: 2024/10/21 21:26
 * Motto: Where there is a will, there is a way!
 * Description:
 * Version: V1.0
 */
public class DateConverter implements Converter<String,Date> {
    @Override
    public Date convert(String s) {
        try{
            if (s.trim().length() == 10) {
                SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
                return df1.parse(s);
            }
            SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return df2.parse(s);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("日期转换失败!");
        }
    }
}
